package com.glsc.ngateway.platform.controller.other;


import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.aspect.UserCache;
import com.glsc.ngateway.platform.domain.Vpn;
import com.glsc.ngateway.platform.service.other.VpnService;
import com.glsc.ngateway.platform.utils.report.ReportFileConfigEnum;
import com.glsc.ngateway.platform.utils.report.ReportUploadContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author zhouzp
 * @date 2020/8/19
 * 系统license管理
 */
@RestController
@RequestMapping("/api/vpn")
@Api(value = "vpn管理", tags = {"vpn管理"})
public class VpnController {

    private static Logger logger = LoggerFactory.getLogger(VpnController.class);

    @Resource
    private VpnService vpnService;
    @Autowired
    private UserCache userCache;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询
     * @param params
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/search")
    @ApiOperation(value = "查询", notes = "查询")
    public PlatformResponse<Object> search(@RequestBody Object params) {
        Object result = vpnService.search(params);
        return PlatformResponse.successData(result);
    }

    @PostMapping("/uploadUser")
    @ApiOperation(value = "导入", notes = "导入")
    public PlatformResponse<?> uploadUser(@RequestPart("file") MultipartFile file) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<Vpn, String> context = vpnService.upload(file, user);
        return context.isCheckPass() ? PlatformResponse.successData("成功") : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    @PostMapping("/uploadResource")
    @ApiOperation(value = "导入", notes = "导入")
    public PlatformResponse<?> uploadResource(@RequestPart("file") MultipartFile file) throws IOException {
        PlatformUserDto user = userCache.get(request);
        ReportUploadContext<Vpn, String> context = vpnService.uploadResource(file, user);
        return context.isCheckPass() ? PlatformResponse.successData("成功") : PlatformResponse.failedMsg(String.join(",", context.getCheckRes()));
    }

    /**
     * 导出数据
     */
    @PostMapping("/export")
    @ApiOperation(value = "导出报表", notes = "导出报表")
    public byte[] export(@RequestBody Vpn param, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;" + "filename=" + URLEncoder.encode(ReportFileConfigEnum.VPN_EXPORT.getFileName(), "UTF-8"));
        return vpnService.export(param);
    }


}
